<template>
  <view> </view>


</template>

<script setup>
import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";
import { getSettingMall } from "@/services/home";
import { updateInfo } from "@/services/biscuit";
import { ossImg } from "@/utils/utils";

import { userLogin } from "@/services/utils";
import privacyPopup from "@/components/privacy-popup";

const src = ref("");
onLoad(async (options) => {
  console.log(options);
  getApp().accessLogAdd();
  if (!uni.getStorageSync("TOKEN")) {
    // 静默拿取微信用户的 openId 与 unionId 信息
    uni.login({
      provider: "weixin",
      success: async (res) => {
        console.log(res);
        const loginParam = {
          type: 1,
        };
        loginParam.code = res.code;
        const { data } = await userLogin(loginParam);
        uni.setStorageSync("TOKEN", data?.accessToken);
        uni.setStorageSync("OPENID", data?.openId);
        uni.setStorageSync("UNIONID", data?.unionId);
        getApp().emdlz("set", "user", "openid", data?.openId);
        getApp().emdlz("set", "user", "unionId", data?.unionId);
        getApp().sr().track("login_wxapp", {
          // more...
        });
        getApp()
          .getUserInfo()
          .then(async () => {
            if (
              getApp().globalData.user.nickname === null ||
              getApp().globalData.user.nickname === "" ||
              getApp().globalData.user.nickname === undefined
            ) {
              const up = await updateInfo({
                nickname: "微信用户",
                icon: ossImg("personal/moren_avatar.png"),
              });
            }
          });

        const getUrl = await getSettingMall({ key: "third_activity" });
        if (getUrl?.code === 200) {
          setTimeout(() => {
            uni.redirectTo({
              url: `/pages/webView/luckyDrawH5?webSrc=${getUrl.data.lotteryUrl}&accesstoken=${data.accessToken}&openId=${data.openId}`,
            });
            // uni.redirectTo({
            //   url: `/pages/cnyPage/index`,
            // });
          }, 1500);
        }

        getApp().sr().track("login_wxapp", {
          // more...
        });
      },
    });
  } else {
    if (
      getApp().globalData.user.nickname === null ||
      getApp().globalData.user.nickname === "" ||
      getApp().globalData.user.nickname === undefined
    ) {
      const up = await updateInfo({
        nickname: "微信用户",
        icon: ossImg("personal/moren_avatar.png"),
      });
    }
    const getUrl = await getSettingMall({ key: "third_activity" });
    if (getUrl?.code === 200) {
      setTimeout(() => {
        uni.redirectTo({
          url: `/pages/webView/luckyDrawH5?webSrc=${
            getUrl.data.lotteryUrl
          }&accesstoken=${uni.getStorageSync(
            "TOKEN"
          )}&openId=${uni.getStorageSync("OPENID")}&banner=1`,
        });
        // uni.redirectTo({
        //   url: `/pages/cnyPage/index`,
        // });
      }, 1500);
    }
  }
  console.log(src.value);
});
</script>

<style></style>
